package cn.iocoder.yudao.module.system.service.userpointslog;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.system.controller.admin.userpointslog.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.userpointslog.UserPointsLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.system.dal.mysql.userpointslog.UserPointsLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;

/**
 * 用户积分操作日志 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserPointsLogServiceImpl implements UserPointsLogService {

    @Resource
    private UserPointsLogMapper userPointsLogMapper;

    @Override
    public Long createUserPointsLog(UserPointsLogSaveReqVO createReqVO) {
        // 插入
        UserPointsLogDO userPointsLog = BeanUtils.toBean(createReqVO, UserPointsLogDO.class);
        userPointsLogMapper.insert(userPointsLog);
        // 返回
        return userPointsLog.getId();
    }

    @Override
    public void updateUserPointsLog(UserPointsLogSaveReqVO updateReqVO) {
        // 校验存在
        validateUserPointsLogExists(updateReqVO.getId());
        // 更新
        UserPointsLogDO updateObj = BeanUtils.toBean(updateReqVO, UserPointsLogDO.class);
        userPointsLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserPointsLog(Long id) {
        // 校验存在
        validateUserPointsLogExists(id);
        // 删除
        userPointsLogMapper.deleteById(id);
    }

    private void validateUserPointsLogExists(Long id) {
        if (userPointsLogMapper.selectById(id) == null) {
            throw exception(USER_POINTS_LOG_NOT_EXISTS);
        }
    }

    @Override
    public UserPointsLogDO getUserPointsLog(Long id) {
        return userPointsLogMapper.selectById(id);
    }

    @Override
    public PageResult<UserPointsLogDO> getUserPointsLogPage(UserPointsLogPageReqVO pageReqVO) {
        return userPointsLogMapper.selectPage(pageReqVO);
    }

}